<?php

class TradeCreateController extends BaseController
{
	
	/**
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 */
	public function actionCreate()
	{
		$model=new Trade;
		$model->ztr_type=1;//类型:1求购,2供应,3代理,4加工

		//ajax验证
		$this->performAjaxValidation($model);

		if(isset($_POST['Trade']))
		{
			$model->attributes=$_POST['Trade'];
			$model->ztr_exp_time = strtotime($model->ztr_exp_time);
			if($_POST['cateid3']>0){
				$model->zca_id = (int)$_POST['cateid3'];
			}elseif($_POST['cateid2']>0){
				$model->zca_id = (int)$_POST['cateid2'];
			}elseif($_POST['cateid1']>0){
				$model->zca_id = (int)$_POST['cateid1'];
			}else{
				//失败跳转
				$this->render('../index/error',array('message'=>'行业分类不能为空，请选择行业分类！'));
				exit;
			}
			
			if($model->save()){
				//成功跳转
				$this->render('../index/success',array(    
		            'message'=>'恭喜，宝贝信息发布成功',    
		            'links'=>array(    
		            		array('求购管理',$this->pre_module_url.'/trade/admin'),
		            		array('继续发布求购',$this->pre_module_url.'/tradeCreate/create'),
		                    array('浏览发布的求购',$this->pre_module_url.'/trade/detal/id/'.$model->ztr_id),        
		                   ),    
					));    
				exit;  
			}
		}

		$this->render('create',array(
			'model'=>$model,
		));
	}

	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='trade-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
}